package com.dycx.api.controller.app.user;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.cms.dto.ArticleDTO;
import com.dycx.cms.service.ArticleService;
import com.dycx.cms.vo.ArticleVO;
import com.dycx.common.RestResponse;
import com.dycx.common.annotation.Authorization;
import com.dycx.common.context.UserInfoContext;
import com.dycx.user.dto.CollectDTO;
import com.dycx.user.service.CollectService;
import com.dycx.user.service.UserAuthService;
import com.dycx.user.vo.CollectVO;
import com.dycx.user.vo.UserVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 收藏(Collect)表控制层
 *
 * @author wuzongbin
 * @since 2023-12-29 10:53:24
 */
@RestController
@RequestMapping(value = "/app/user/collect")
public class CollectApiController {
    /**
     * 服务对象
     */
    @Resource
    private CollectService collectService;

    @Resource
    private ArticleService articleService;

    @Resource
    private UserAuthService userAuthService;

    /**
     * 分页
     **/
    @Authorization
    @PostMapping("getPage")
    public RestResponse<Page<CollectVO>> getPage(@RequestBody CollectDTO dto, HttpServletRequest request) {
        dto.setUid(UserInfoContext.get().getUserId());
        Page<CollectVO> page = collectService.getPage(dto);
        if (page.getRecords().size() == 0) return RestResponse.success(page);

        List<Integer> ids = page.getRecords().stream().map(CollectVO::getBizId).collect(Collectors.toList());
        ArticleDTO articleDTO = new ArticleDTO();
        articleDTO.setPageIndex(1);
        articleDTO.setPageSize(dto.getPageSize());
        articleDTO.setIds(ids);
        Page<ArticleVO> articlePage = articleService.getPage(articleDTO);

        String token = request.getHeader("authorization");
        UserVO userVO = userAuthService.getTokenInfo(token);
        articlePage.setRecords(articleService.setAppData(articlePage.getRecords(), userVO));

        page.getRecords().forEach(item -> {
            articlePage.getRecords().forEach(item2 -> {
                if (item.getBizId().equals(item2.getId())) {
                    item.setInfo(BeanUtil.toBean(item2, Map.class));
                }
            });
        });

        return RestResponse.success(page);
    }

    /**
     * 添加
     **/
    @Authorization
    @PostMapping("create")
    public RestResponse<Boolean> create(@RequestBody @Valid CollectDTO dto) {
        dto.setUid(UserInfoContext.get().getUserId());
        return collectService.create(dto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 删除
     **/
    @Authorization
    @PostMapping("delete")
    public RestResponse<Boolean> delete(@RequestBody @Valid CollectDTO dto) {
        dto.setUid(UserInfoContext.get().getUserId());
        return collectService.delete(dto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 删除
     **/
    @Authorization
    @PostMapping("getIsCollect")
    public RestResponse<Boolean> getIsCollect(@RequestBody @Valid CollectDTO dto) {
        dto.setUid(UserInfoContext.get().getUserId());
        return RestResponse.success(collectService.getIsCollect(dto));
    }

}

